package cn.yh.hospitalappointmentsystemdemo.service.impl;

import cn.yh.hospitalappointmentsystemdemo.domain.entity.DoctorSchedule;
import cn.yh.hospitalappointmentsystemdemo.domain.vo.ScheduleVo;
import cn.yh.hospitalappointmentsystemdemo.mapper.DoctorScheduleMapper;
import cn.yh.hospitalappointmentsystemdemo.mapper.OrdersMapper;
import cn.yh.hospitalappointmentsystemdemo.service.IDoctorScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorScheduleServiceImpl extends ServiceImpl<DoctorScheduleMapper, DoctorSchedule> implements IDoctorScheduleService {
    @Autowired
    private DoctorScheduleMapper doctorScheduleMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<ScheduleVo> getDoctorSchedule( Long doctorId) {
        return doctorScheduleMapper.getDoctorSchedule(doctorId);
    }

    @Override
    public boolean refreshSchedule() {
        // Step 1: 逻辑删除 8天之外的数据
        System.out.println("Step 1: 开始删除8天之外的数据");
        try {
            doctorScheduleMapper.logicDeleteOutdated();
        } catch (Exception e) {
            return false;
        }

        // Step 2: 插入或恢复最近8天有挂号记录的排班
        System.out.println("Step 2: 开始插入或恢复最近8天有挂号记录的排班");
        try {
            doctorScheduleMapper.insertMissingSchedules();
        } catch (Exception e) {
            return false;
        }

        // Step 3: 更新每条排班的可预约数（available_slots）
        System.out.println("Step 3: 开始更新每条排班的可预约数");
        try {
            doctorScheduleMapper.updateAvailableSlots();
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
